# Create your views here.

from django.views import View

from model.models import Point
from server.utils import Response, Request, LoginRequiredPerson, Verify, Code


class PointView(LoginRequiredPerson, View):
    """ User operation points """

    def get(self, request):
        """ Get points uploaded by users """
        try:
            type = int(request.GET.get('type', -1))
            status = int(request.GET.get('status', -1))
        except Exception as e:
            return Response.Json(code=Code.BadRequest)

        points = Point.objects.filter(uploader=request.user)
        if type in Point.Type:
            points = points.filter(type=type)
        if status in Point.Status:
            points = points.filter(status=status)

        points = [point.info() for point in points]
        return Response.Json(points=points)

    @Request.getJson
    def post(self, request):
        """ User uploads custom points """
        name = request.JSON.get('name')
        detail = request.JSON.get('detail')
        longitude = request.JSON.get('longitude')
        latitude = request.JSON.get('latitude')
        direction = request.JSON.get('direction', '')

        for code in [Verify.pointName(name), Verify.pointDetail(detail),
                     Verify.longitude(longitude), Verify.latitude(latitude), Verify.direction(direction)]:
            if code != Code.OK:
                return Response.Json(code=code)

        count = Point.objects.filter(uploader=request.user, type=Point.Type.person).count()
        if count >= 5000:
            return Response.Json(code=Code.Error, msg="Creation failed, personal point limit is 5000")

        point = Point(name=name, detail=detail, longitude=longitude, latitude=latitude, direction=direction,
                      uploader=request.user)
        point.save()
        return Response.Json(pid=point.pid)

    @Request.getJson
    def put(self, request):
        """ User set points """
        pid = request.JSON.get('pid')
        name = request.JSON.get('name')
        detail = request.JSON.get('detail')
        longitude = request.JSON.get('longitude')
        latitude = request.JSON.get('latitude')
        direction = request.JSON.get('direction')

        code = Verify.pointPid(pid)
        if code != Code.OK:
            return Response.Json(code=code)

        point = Point.objects.get(pid=pid)
        if name != None:
            code = Verify.pointName(name)
            if code != Code.OK:
                return Response.Json(code=code)
            point.name = name
        if detail != None:
            code = Verify.pointDetail(detail)
            if code != Code.OK:
                return Response.Json(code=code)
            point.detail = detail
        if longitude != None:
            code = Verify.longitude(longitude)
            if code != Code.OK:
                return Response.Json(code=code)
            point.longitude = longitude
        if latitude != None:
            code = Verify.latitude(latitude)
            if code != Code.OK:
                return Response.Json(code=code)
            point.latitude = latitude
        if direction != None:
            code = Verify.direction(direction)
            if code != Code.OK:
                return Response.Json(code=code)
            point.direction = direction

        point.save()
        return Response.Json()
